//
//  MainTabBarController.m
//  weather
//
//  Created by 大L君的无敌战舰 on 16/3/31.
//  Copyright © 2016年 bigL.com. All rights reserved.
//

#import "MainTabBarController.h"
#import "MainTabBarView.h"

#import "LiveViewController.h"
#import "ProfileTableViewController.h"
#import "WeatherCollectionViewController.h"





@interface MainTabBarController ()<MainTabBarViewDelegate>

/*存放tabBarItem内容的可变数组*/
@property (strong, nonatomic) NSMutableArray * tabBarItemArray;
@end

@implementation MainTabBarController

/**
 *  懒加载tabBarItemArray对象
 */
- (NSMutableArray *)tabBarItemArray
{
    if (!_tabBarItemArray) {
        _tabBarItemArray = [NSMutableArray array];
    }
    return _tabBarItemArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    //  自定义TabBar
    [self TabBarWithCustom];
    //  加载子控制器
    [self childViewControllerLoad];
}





/**
 *  自定义TabBar
 */
- (void)TabBarWithCustom
{
    //移除默认tabbar
    [self.tabBar removeFromSuperview];
    //添加自定义tabBar
    MainTabBarView *tabBar = [[MainTabBarView alloc]initWithFrame:self.tabBar.frame];
    //传递tabBarItem中的数据
    tabBar.tabBarItemArray = self.tabBarItemArray;
    [self.view addSubview:tabBar];
    //设置代理
    tabBar.delegate        = self;
    
    
}

#pragma mark - MainTabBarViewDelegate
-(void)MainTabBarViewButton:(MainTabBarView *)tabBarView
             selectBtnIndex:(NSInteger)index
{
    self.selectedIndex = index;
}


/**
 *  加载TabBarController子控制器
 */
- (void)childViewControllerLoad
{
    //天气VC
    WeatherCollectionViewController *weatherVC = [[WeatherCollectionViewController alloc]init];
    weatherVC.view.backgroundColor = [UIColor clearColor];
    UIImage *weatherImage          = [UIImage imageNamed:@"tabbar_weather_normal"];
    UIImage *weatherSelectImage    = [UIImage imageNamed:@"tabbar_weather_select"];
    [self addSingleViewController:weatherVC title:@"天气" image:weatherImage selectImage:weatherSelectImage];
    
    //时景VC
    LiveViewController *liveVC  = [[LiveViewController alloc]init];
    liveVC.view.backgroundColor = BLRandomColor(2);
    UIImage *liveImage          = [UIImage imageNamed:@"tabbar_live_normal"];
    UIImage *liveSelectImage    = [UIImage imageNamed:@"tabbar_live_select"];
    [self addSingleViewController:liveVC title:@"时景" image:liveImage selectImage:liveSelectImage];
    
    
    //我的资料VC
    ProfileTableViewController *profileVC = [[ProfileTableViewController alloc]init];
    profileVC.view.backgroundColor   = BLRandomColor(3);
    UIImage *profileImage            = [UIImage imageNamed:@"tabbar_profile_normal"];
    UIImage *profileSelectImage      = [UIImage imageNamed:@"tabbar_profile_select"];
    [self addSingleViewController:profileVC title:@"我" image:profileImage selectImage:profileSelectImage];
    
    
}

/**
 *  添加单个控制器
 *
 *  @param VC          子控制器
 *  @param title       tabBar的标题
 *  @param image       普通状态图片
 *  @param selectImage 选中状态图片
 */
- (void)addSingleViewController:(UIViewController *)VC
                          title:(NSString *)title
                          image:(UIImage *)image
                    selectImage:(UIImage *)selectImage
{
    VC.tabBarItem.title         = title;
    VC.tabBarItem.image         = image;
    VC.tabBarItem.selectedImage = selectImage;
    [self.tabBarItemArray addObject:VC.tabBarItem];
    
    //判断VC包装导航条
    if ([VC isKindOfClass:[WeatherCollectionViewController class]]) {
        
        UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:VC];
        [self addChildViewController:nav];
    }
    else if([VC isKindOfClass:[LiveViewController class]])
    {
        UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:VC];
        [self addChildViewController:nav];
    }
    
    if ([VC isKindOfClass:[ProfileTableViewController class]]) {
        [self addChildViewController:VC];
    }
}




@end
